package com.zhijian.warehouse.manage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.warehouse.manage.dao.StoreLockRecordMapper;
import com.zhijian.warehouse.manage.entity.po.StoreLockRecord;
import com.zhijian.warehouse.manage.entity.vo.request.StoreLockRecordSearchVo;
import com.zhijian.warehouse.manage.entity.vo.request.StoreLockRecordSaveOrUpdateVo;
import com.zhijian.warehouse.manage.entity.vo.response.StoreLockRecordVo;
import com.zhijian.warehouse.manage.service.StoreLockRecordService;
import com.zhijian.util.page.Page;
import com.zhijian.warehouse.manage.util.AssertCommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-10-27 13:44:54
 */
@Slf4j
@Service
public class StoreLockRecordServiceImpl implements StoreLockRecordService {

	@Autowired
    private StoreLockRecordMapper mapper;

    @Override
    public StoreLockRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public StoreLockRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(StoreLockRecord storeLockRecord) {
        Date now = new Date();
        storeLockRecord.setCreateTime(now);
        return mapper.insert(storeLockRecord) > 0 ? storeLockRecord.getId() : 0L;
    }

    @Override
    public Long insertSelective(StoreLockRecord storeLockRecord) {
        Date now = new Date();
        storeLockRecord.setCreateTime(now);
        return mapper.insertSelective(storeLockRecord) > 0 ? storeLockRecord.getId() : 0L;
    }

    @Override
    public boolean update(StoreLockRecord storeLockRecord) {
        Date now = new Date();
        return mapper.updateByPrimaryKey(storeLockRecord) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(StoreLockRecord record) {
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(StoreLockRecordSaveOrUpdateVo saveOrUpdateVo) {
        StoreLockRecord storeLockRecord = new StoreLockRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, storeLockRecord);
        return this.insert(storeLockRecord) != 0;
    }

    @Override
    public boolean modify(StoreLockRecordSaveOrUpdateVo saveOrUpdateVo) {
        StoreLockRecord old = this.selectById(saveOrUpdateVo.getId());
        AssertCommonUtil.notNull(old, "此数据不存在或者已被删除");
        StoreLockRecord storeLockRecord = new StoreLockRecord();
        BeanUtils.copyProperties(saveOrUpdateVo, storeLockRecord);
        return this.update(storeLockRecord);
    }

    @Override
    public StoreLockRecordVo selectOne(StoreLockRecordSearchVo storeLockRecordSearchVo) {
        return mapper.selectOne(storeLockRecordSearchVo);
     }

    @Override
    public Integer count(StoreLockRecordSearchVo storeLockRecordSearchVo) {
        return mapper.count(storeLockRecordSearchVo);
    }

    @Override
    public List<StoreLockRecordVo> selectList(StoreLockRecordSearchVo storeLockRecordSearchVo) {
        return mapper.selectList(storeLockRecordSearchVo);
    }

    @Override
    public Page<StoreLockRecordVo> selectPage(StoreLockRecordSearchVo storeLockRecordSearchVo) {
        AssertCommonUtil.check(Objects.isNull(storeLockRecordSearchVo.getPageNum()) || Objects.isNull(storeLockRecordSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(storeLockRecordSearchVo);
        //分页
        Page<StoreLockRecordVo> page = new Page<>(true, count, storeLockRecordSearchVo.getPageNum(), storeLockRecordSearchVo.getPageSize());
        storeLockRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<StoreLockRecordVo> responseVos = this.selectList(storeLockRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<StoreLockRecord> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}